package com.dg.river.core.util;

import android.content.Context;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.dg.river.R;
import com.dg.river.core.view.pickerview.builder.OptionsPickerBuilder;
import com.dg.river.core.view.pickerview.builder.TimePickerBuilder;
import com.dg.river.core.view.pickerview.listener.OnOptionsCancelListener;
import com.dg.river.core.view.pickerview.listener.OnOptionsSelectListener;
import com.dg.river.core.view.pickerview.listener.OnTimeSelectListener;
import com.dg.river.core.view.pickerview.view.OptionsPickerView;
import com.dg.river.core.view.pickerview.view.TimePickerView;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by hy on 2018/11/9
 * Description : 选择器工具类
 */
public class PickerViewUtils {

    public interface onPickListener {
        void onPick(String options1, String options2, String options3);
    }

    public interface onPickMapListener {
        void onPickMap(Map<String, String> options1, Map<String, String> options2, Map<String, String> options3);
    }

    public interface onPickListenerOnePick {
        void onPick(String options1);
    }

    public interface onTimePickListener {
        void onPick(Date date);
    }

    public interface onPickCancelListener {
        void onPick();
    }

    /**
     * 显示选择器
     *
     * @param mContext       context对象
     * @param options1Items  第一个选项数组
     * @param options2Items  第二个选项数组
     * @param options3Items  第三个选项数组
     * @param onPickListener 监听器
     */
    public static void showPickerView(Context mContext,
                                      final List<String> options1Items,
                                      final List<List<String>> options2Items,
                                      final List<List<List<String>>> options3Items,
                                      final onPickListener onPickListener) {
        OptionsPickerView pvOptions = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                onPickListener.onPick(options1Items.get(options1),
                        options2Items.get(options1).get(option2),
                        options3Items.get(options1).get(option2).get(options3));
            }
        }, new OnOptionsCancelListener() {
            @Override
            public void onOptionsCancel() {

            }
        }).setContentTextSize(16)
                .setTitleSize(16)
                .setSubCalSize(16)
                .setTitleText(mContext.getString(R.string.choose))
                .setTitleColor(mContext.getResources().getColor(R.color.black_141414))
                .setSubmitColor(mContext.getResources().getColor(R.color.red_f13232))
                .setCancelColor(mContext.getResources().getColor(R.color.black_141414))
                .setTitleBgColor(mContext.getResources().getColor(R.color.white))
                .setOutSideCancelable(true)
                .isRestoreItem(true)
                .setLineSpacingMultiplier(2.5f)
                .setTextColorCenter(mContext.getResources().getColor(R.color.black_141414))
                .setTextColorOut(mContext.getResources().getColor(R.color.gray_999999))
                .build();
        pvOptions.setPicker(false, options1Items, options2Items, options3Items);
        pvOptions.show();
    }

    /**
     * 显示选择器
     *
     * @param mContext          context对象
     * @param options1Items     第一个选项数组
     * @param options2Items     第二个选项数组
     * @param options3Items     第三个选项数组
     * @param onPickMapListener 监听器
     */
    public static void showPickerMapView(Context mContext,
                                         final List<Map<String, String>> options1Items,
                                         final List<List<Map<String, String>>> options2Items,
                                         final List<List<List<Map<String, String>>>> options3Items,
                                         final onPickMapListener onPickMapListener,
                                         int defaultSelect, int defaultSelect2, int defaultSelect3) {
        OptionsPickerView pvOptions = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                if (options1Items.size() > 0) {
                    if (options2Items.size() > 0 && options2Items.get(options1).size() > 0) {
                        if (options3Items.size() > 0 && options3Items.get(options1).size() > 0 && options3Items.get(options1).get(option2).size() > 0) {
                            onPickMapListener.onPickMap(options1Items.get(options1),
                                    options2Items.get(options1).get(option2),
                                    options3Items.get(options1).get(option2).get(options3));
                        } else {
                            onPickMapListener.onPickMap(options1Items.get(options1),
                                    options2Items.get(options1).get(option2),
                                    null);
                        }
                    } else {
                        onPickMapListener.onPickMap(options1Items.get(options1), null, null);
                    }
                } else {
                    onPickMapListener.onPickMap(null, null, null);
                }
            }
        }, new OnOptionsCancelListener() {
            @Override
            public void onOptionsCancel() {

            }
        }).setContentTextSize(16)
                .setTitleSize(16)
                .setSubCalSize(16)
                .setTitleText(mContext.getString(R.string.choose))
                .setTitleColor(mContext.getResources().getColor(R.color.black_141414))
                .setSubmitColor(mContext.getResources().getColor(R.color.red_f13232))
                .setCancelColor(mContext.getResources().getColor(R.color.black_141414))
                .setTitleBgColor(mContext.getResources().getColor(R.color.white))
                .setOutSideCancelable(true)
                .isRestoreItem(true)
                .setLineSpacingMultiplier(2.5f)
                .setTextColorCenter(mContext.getResources().getColor(R.color.black_141414))
                .setTextColorOut(mContext.getResources().getColor(R.color.gray_999999))
                .setSelectOptions(defaultSelect, defaultSelect2, defaultSelect3)
                .setOutSideCancelable(false)
                .build();
        pvOptions.setPicker(true, options1Items, options2Items, options3Items);
        pvOptions.show();
    }


    /**
     * 显示选择器 包含Cancel 事件
     *
     * @param mContext          context对象
     * @param options1Items     第一个选项数组
     * @param options2Items     第二个选项数组
     * @param options3Items     第三个选项数组
     * @param onPickMapListener 监听器
     */
    public static void showPickerMapViewHasCancel(Context mContext,
                                                  final List<Map<String, String>> options1Items,
                                                  final List<List<Map<String, String>>> options2Items,
                                                  final List<List<List<Map<String, String>>>> options3Items,
                                                  final onPickMapListener onPickMapListener,
                                                  final onPickCancelListener onPickCancelListener,
                                                  int defaultSelect, int defaultSelect2, int defaultSelect3) {
        OptionsPickerView pvOptions = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                if (options1Items.size() > 0) {
                    if (options2Items.size() > 0 && options2Items.get(options1).size() > 0) {
                        if (options3Items.size() > 0 && options3Items.get(options1).size() > 0 && options3Items.get(options1).get(option2).size() > 0) {
                            onPickMapListener.onPickMap(options1Items.get(options1),
                                    options2Items.get(options1).get(option2),
                                    options3Items.get(options1).get(option2).get(options3));
                        } else {
                            onPickMapListener.onPickMap(options1Items.get(options1),
                                    options2Items.get(options1).get(option2),
                                    null);
                        }
                    } else {
                        onPickMapListener.onPickMap(options1Items.get(options1), null, null);
                    }
                } else {
                    onPickMapListener.onPickMap(null, null, null);
                }
            }
        }, new OnOptionsCancelListener() {
            @Override
            public void onOptionsCancel() {
                if (onPickCancelListener != null) {
                    onPickCancelListener.onPick();
                }
            }
        }).setContentTextSize(16)
                .setTitleSize(16)
                .setSubCalSize(16)
                .setTitleText(mContext.getString(R.string.choose))
                .setTitleColor(mContext.getResources().getColor(R.color.black_141414))
                .setSubmitColor(mContext.getResources().getColor(R.color.red_f13232))
                .setCancelColor(mContext.getResources().getColor(R.color.black_141414))
                .setTitleBgColor(mContext.getResources().getColor(R.color.white))
                .setOutSideCancelable(true)
                .isRestoreItem(true)
                .setLineSpacingMultiplier(2.5f)
                .setTextColorCenter(mContext.getResources().getColor(R.color.black_141414))
                .setTextColorOut(mContext.getResources().getColor(R.color.gray_999999))
                .setSelectOptions(defaultSelect, defaultSelect2, defaultSelect3)
                .setOutSideCancelable(false)
                .build();
        pvOptions.setPicker(true, options1Items, options2Items, options3Items);
        pvOptions.show();
    }

    /**
     * 显示选择器
     *
     * @param mContext       context对象
     * @param title          标题
     * @param options1Items  第一个选项数组
     * @param onPickListener 监听器
     */
    public static void showPickerView(Context mContext,
                                      String title,
                                      final List<String> options1Items,
                                      final onPickListenerOnePick onPickListener, int defaultSelect) {
        OptionsPickerView pvOptions = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                onPickListener.onPick(options1Items.get(options1));
            }
        }, new OnOptionsCancelListener() {
            @Override
            public void onOptionsCancel() {

            }
        }).setContentTextSize(16)
                .setTitleSize(16)
                .setSubCalSize(16)
                .setTitleText(title)
                .setTitleColor(mContext.getResources().getColor(R.color.black_141414))
                .setSubmitColor(mContext.getResources().getColor(R.color.red_f13232))
                .setCancelColor(mContext.getResources().getColor(R.color.black_141414))
                .setTitleBgColor(mContext.getResources().getColor(R.color.white))
                .setOutSideCancelable(true)
                .isRestoreItem(true)
                .setLineSpacingMultiplier(2.5f)
                .setTextColorCenter(mContext.getResources().getColor(R.color.black_141414))
                .setTextColorOut(mContext.getResources().getColor(R.color.gray_999999))
                .setSelectOptions(defaultSelect)
                .build();
        pvOptions.setPicker(false, options1Items, null, null);
        pvOptions.show();
    }

    /**
     * 显示时间选择器 - 默认当前时间
     *
     * @param mContext
     * @param onTimePickListener
     */
    public static void showTimePicker(Context mContext, String title,
                                      final onTimePickListener onTimePickListener, Calendar selectDate) {
        TimePickerBuilder timePickerBuilder = new TimePickerBuilder(mContext, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                onTimePickListener.onPick(date);
            }
        }).setLineSpacingMultiplier(2f)
                .setContentTextSize(16)
                .setTitleSize(16)
                .setSubCalSize(16)
                .setTitleText(title)
                .setTitleColor(mContext.getResources().getColor(R.color.black_141414))
                .setSubmitColor(mContext.getResources().getColor(R.color.red_f13232))
                .setCancelColor(mContext.getResources().getColor(R.color.black_141414))
                .setTitleBgColor(mContext.getResources().getColor(R.color.white))
                .setOutSideCancelable(true)
                .setTextColorCenter(mContext.getResources().getColor(R.color.black_141414))
                .setTextColorOut(mContext.getResources().getColor(R.color.gray_999999));
        if (selectDate != null) {
            timePickerBuilder.setDate(selectDate);
        }
        TimePickerView build = timePickerBuilder.build();
        build.show();
    }

    /**
     * 显示时间选择器 - 默认当前时间 商家入驻用到，显示时分 结束时间
     *
     * @param mContext
     * @param onTimePickListener
     */
    public static void showTimeDivisionEndPicker(Context mContext, String title,
                                                 final onTimePickListener onTimePickListener, Calendar startDate, Calendar endDate) {
        TimePickerBuilder timePickerBuilder = new TimePickerBuilder(mContext, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                onTimePickListener.onPick(date);
            }
        }).setLineSpacingMultiplier(3f)
                .setContentTextSize(16)
                .setTitleSize(16)
                .setSubCalSize(16)
                .setTitleText(title)
                .setTitleColor(Utils.getColor(mContext, R.color.black_464646))
                .setSubmitColor(Utils.getColor(mContext, R.color.blue_008cff))
                .setCancelColor(Utils.getColor(mContext, R.color.gray_767e89))
                .setTitleBgColor(Utils.getColor(mContext, R.color.white))
                .isDialog(true)
                .setOutSideCancelable(true)
                //年月日时分秒 的显示与否，不设置则默认全部显示
                .setType(new boolean[]{true, true, true, false, false, false})
                .setLabel("年", "月", "日", "", "", "")
                .setTextColorCenter(Utils.getColor(mContext, R.color.blue_008cff))
                .setTextColorOut(Utils.getColor(mContext, R.color.gray_b9b9b9));
        if (endDate != null) {
            timePickerBuilder.setDate(endDate);
        }
        TimePickerView build = timePickerBuilder.build();
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
        params.leftMargin = 0;
        params.rightMargin = 0;
        ViewGroup contentContainer = build.getDialogContainerLayout();
        contentContainer.setLayoutParams(params);
        build.getDialog().getWindow().setGravity(Gravity.BOTTOM);//可以改成Bottom
        build.getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        build.show();
    }
}
